<?php
class IndexController extends Zendme_Controller_Action {
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array('itemCountPerPage' => 8,'pageRange' => 4); 
															  
	protected $_namespace;
	
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];
		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';	

							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->keywords 	= '';
		}
		$this->_arrParam['ssFilter']['keywords'] 	= $ssFilter->keywords;
		
		$info = new Zendme_System_Info();
		
		$language = $info->getLanguage('admin');
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);	
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
	
		Zend_Layout::getMvcInstance()->assign('bg', 'bag-image-page');	
		
		$template_path = TEMPLATE_PATH . "/front/system1";
		$this->loadTemplate($template_path,'template.ini','template');	
	}		
	public function indexAction(){	
	//	$this->_forward('index','index','news');	
		Zend_Layout::getMvcInstance()->assign('bg', 'bag-image-main');
		Zend_Layout::getMvcInstance()->assign('current', 'home');
		
		
		$this->view->headTitle('Cty Cổ Phần Hội Doanh Nghiệp Quận 9');
		$this->view->headMeta()->appendName('description', ''); 
		$this->view->headMeta()->appendName('keywords', ''); 	
	}
	
	public function pageAction()
	{	
		
		$mdlPage=new Zendme_Models_Pages();
		$page=$mdlPage->getItem($this->_arrParam,'public');
		$this->view->page=$page;
		
		$this->view->headTitle($page['name']);	
		Zend_Layout::getMvcInstance()->assign('current', $this->_arrParam['slug']);
	}

	public function contactAction(){
		$this->view->Title = 'Liên hệ';
		$this->view->headTitle($this->view->Title,true);			
		$captcha = new Zendme_Captcha_Image();
		//9.Truy gia tri cua CAPTCHA ra VIEW
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
			
		if($this->_request->isPost()){
			$captchaID = $this->_arrParam['captchaID'];		
			$validator = new Default_Form_ValidateContact($this->_arrParam);
			if($validator->isError() == true){				
				$this->view->messageError = $validator->getMessageError();
				$this->view->Item = $validator->getData();		
			}
			else{
//				$arrParam = $validator->getData();Ư
//				$mail = new Zendme_Mail();
//				$success = $mail->sendContact($arrParam,null);
//				if($success){
//					$this->_helper->flashMessenger->addMessage('Chúng tôi đã nhận được liên hệ của bạn. Chúng tôi sẽ gửi email hồi đáp trong vòng 24h nữa. Chúc bạn một ngày tốt đẹp.');
//					$this->_helper->_redirector('contact');
//				}else{
//					$this->_helper->flashMessenger->addMessage('Lỗi! Không thể gửi email được, vui lòng liên hệ BQT để được trợ giúp!');
//					$this->_helper->_redirector('contact');
//				}			
				
				$mdlInbox=new Zendme_Models_Inbox();
				$mdlInbox->saveItem($this->_arrParam);
				
				$this->view->flagSuccess="<span class='success'>Bạn đã gửi thành công !</span>";
				
			}
			//$captcha->removeImg($captchaID);		
		}
				
		//$this->view->messages = $this->_helper->flashMessenger->getMessages();
		//$captcha->removeImg($this->view->captcha_id,array('exception'=>true));
		
		Zend_Layout::getMvcInstance()->assign('current', 'contact');
		$mdlPage=new Zendme_Models_Pages();
		$this->_arrParam['slug']='lien-he';
		$page=$mdlPage->getItem($this->_arrParam,'public');
		$this->view->page=$page;
	}
	
	public function captchaImageAction(){
		$this->_helper->layout->disableLayout();
		$captcha = new Zendme_Captcha_Image();
		$captchaID = $this->_request->getParam('captchaID',0);
		$captcha->removeImg($captchaID);
				
		$this->view->captcha = $captcha->render($this->view);
		$this->view->captcha_id = $captcha->getId();
	}
}
